package sdk.webview.fmc.com.fmcsdk.service;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.support.v4.util.ArrayMap;
import android.util.Log;

import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import sdk.webview.fmc.com.fmcsdk.base.BaseData;
import sdk.webview.fmc.com.fmcsdk.base.UserApi;
import sdk.webview.fmc.com.fmcsdk.bean.UploadImage;
import sdk.webview.fmc.com.fmcsdk.bean.UploadVideoBean;
import sdk.webview.fmc.com.fmcsdk.bean.WorkOrderNumBean;
import sdk.webview.fmc.com.fmcsdk.db.DBManager;
import sdk.webview.fmc.com.fmcsdk.db.IMAGE;
import sdk.webview.fmc.com.fmcsdk.db.Record;
import sdk.webview.fmc.com.fmcsdk.db.VIDEO;
import sdk.webview.fmc.com.fmcsdk.util.Constant;
import sdk.webview.fmc.com.fmcsdk.util.FastClickHelper;
import sdk.webview.fmc.com.fmcsdk.util.FastClickHelper222;
import sdk.webview.fmc.com.fmcsdk.util.HandleJsEvent;
import sdk.webview.fmc.com.fmcsdk.util.ImageCompressor;
import sdk.webview.fmc.com.fmcsdk.util.ImageUtils;
import sdk.webview.fmc.com.fmcsdk.util.KLog;
import sdk.webview.fmc.com.fmcsdk.util.NetworkUtils;
import sdk.webview.fmc.com.fmcsdk.util.Url;
import sdk.webview.fmc.com.fmcsdk.util.UserHelper;
import sdk.webview.fmc.com.fmcsdk.util.Utility;
import sdk.webview.fmc.com.fmcsdk.util.XLog;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ApiFactory;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.ResponseTransformer;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.observer.RetrofitFactory;
import sdk.webview.fmc.com.fmcsdk.util.retrofit2.subscriber.ResponseSubscriber;
import top.zibin.luban.OnCompressListener;

public class UploadFileManager {
    private volatile static UploadFileManager singleton;

    private UploadFileManager() {
    }

    public static UploadFileManager getInstance() {
        if (singleton == null) {
            synchronized (UploadFileManager.class) {
                if (singleton == null) {
                    singleton = new UploadFileManager();
                }
            }
        }
        return singleton;
    }


    /**
     * 查询数据库是否有图片
     *
     * @param iswifiup 是否为wifi连接状态
     */
    List<Record> imagList;
    private boolean iswifiupImage = false;

    List<Record> vedioList;
    private boolean iswifiupVideo = false;

    //图片的数量
    private int imageListSize = 0;

    private Context mContext;

    private SharedPreferences defaultSharedPreferences;

    private UploadSuccessBack mUploadSuccessBack;

    /**
     * 查询数据库是否有图片和视频，并开启心跳
     *
     * @param iswifiupim
     * @param iswifiupvi
     */

    public void startQueryFile(Context context, boolean iswifiupim, boolean iswifiupvi, UploadSuccessBack uploadSuccessBack) {
       KLog.e("11111");
        iswifiupImage = iswifiupim;
        iswifiupVideo = iswifiupvi;
        mContext = context;
        mUploadSuccessBack = uploadSuccessBack;
        defaultSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext);

        imagList = DBManager.getDBManager(mContext).getRecords(IMAGE.TABLE_NAME, null);
        vedioList = DBManager.getDBManager(mContext).getRecords(VIDEO.TABLE_NAME, null);
        KLog.e(imagList.size());

        allFileSize = imagList.size() + vedioList.size(); //要上传的文件的整的数目
        mCurMeiZuNum = 10; //当前每一组文件的数目 的默认值 根据后端字典来

        if (imagList.size() > 0) {
            imageListSize = imagList.size();
            if (iswifiupImage) {
                // 开始心跳
                initMeiyizudeNum();
                xintiaoHand();
            }
        } else {
            if (vedioList.size() > 0) {
                if (iswifiupVideo) {
                    // 开始心跳
                    initMeiyizudeNum();
                    xintiaoHand();
                }
            }
        }
    }

    private void uploadImageNew(final Record record, boolean isLast) {
        KLog.e("11111");
        File files = new File(ImageUtils.getH5ImagePath(record.getString("imageUrl"), mContext).replace("jpg", "jpeg"));
        if (!files.exists()) {
            return;
        }

        new ImageCompressor(mContext).compressImageInPlace(files.getPath(), new OnCompressListener() {
            @Override
            public void onStart() {

            }
            @Override
            public void onSuccess(File file) {
                //deleteFiles(files);
                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
                RequestBody typeBody = RequestBody.create(MediaType.parse("multipart/form-data"), record.getString(IMAGE.TIMESTAMP));
                RequestBody timeBody = RequestBody.create(MediaType.parse("multipart/form-data"), System.currentTimeMillis()+"");//,+""

                ApiFactory.getFactory().create(UserApi.class).uploadImgForClient(defaultSharedPreferences.getString(Constant.BASE_ADDRESS_URL, "") + Url.UPLOAD_IMG,
                                Url.XMLHTTPREQUEST,
                                UserHelper.getToken(),
                                requestBody,
                                typeBody,
                                timeBody)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<UploadImage>() {
                            @Override
                            public void onCompleted() {
                                XLog.i("uploadImage result：" + "onCompleted", mContext);
//                        uploadCallBack(record.getString("timeStamp"), "reload123_android:成功", "", null);
                            }

                            @Override
                            public void onError(Throwable e) {
//                        uploadCallBack(record.getString("ownerId"), "reload123_android:失败-->" + e.getClass().getName(), "dos", record);
                                XLog.i("uploadImage result：" + "timeStamp:" + e, mContext);
//                                if(Constant.isHaveNet){
//                                    //mHandler.sendEmptyMessage(101); //上传失败
//                                    EventBus.getDefault().post(new HandleJsEvent(3002, record.getString(IMAGE.OWNERID)));
//                                }
                                if(NetworkUtils.isAvailable(mContext)){
                                    //mHandler.sendEmptyMessage(101); //上传失败
                                    EventBus.getDefault().post(new HandleJsEvent(3002, record.getString(IMAGE.OWNERID)));
                                }
                            }

                            @Override
                            public void onNext(UploadImage result) {
                              //  deleteFiles(file);
                                uploadFilesService(record, result.getRecord().get(0).getId(), 0, isLast);
                            }
                        });

            }

            @Override
            public void onError(Throwable e) {

                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), files);
                RequestBody typeBody = RequestBody.create(MediaType.parse("multipart/form-data"), record.getString(IMAGE.TIMESTAMP));
                RequestBody timeBody = RequestBody.create(MediaType.parse("multipart/form-data"), System.currentTimeMillis()+"");//,+""

                ApiFactory.getFactory().create(UserApi.class).uploadImgForClient(defaultSharedPreferences.getString(Constant.BASE_ADDRESS_URL, "") + Url.UPLOAD_IMG, Url.XMLHTTPREQUEST, UserHelper.getToken(), requestBody, typeBody, timeBody)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Subscriber<UploadImage>() {
                            @Override
                            public void onCompleted() {
                                XLog.i("uploadImage result：" + "onCompleted", mContext);
//                        uploadCallBack(record.getString("timeStamp"), "reload123_android:成功", "", null);
                            }

                            @Override
                            public void onError(Throwable e) {
                                deleteFiles(files);
//                        uploadCallBack(record.getString("ownerId"), "reload123_android:失败-->" + e.getClass().getName(), "dos", record);
                                XLog.i("uploadImage result：" + "timeStamp:" + e, mContext);
                               // mHandler.sendEmptyMessage(101); //上传失败
                                EventBus.getDefault().post(new HandleJsEvent(3002, record.getString(IMAGE.OWNERID)));
                            }

                            @Override
                            public void onNext(UploadImage result) {
                                deleteFiles(files);
                                uploadFilesService(record, result.getRecord().get(0).getId(), 0, isLast);
                            }
                        });
            }
        });


    }

    private void deleteFiles(File dir) {
        if (dir == null || !dir.exists() || dir.listFiles() == null) return;
        for (File file : dir.listFiles()) {
            if (file.isDirectory()) {
                deleteFiles(file);
            } else {
                file.delete();
            }
        }
        dir.delete();
    }

    /**
     * 上传图片到客户端
     */

    private void xintiaoHand() {
        if (!FastClickHelper222.isFastClick()) return;
        KLog.e("2222222222", "xintiaoHand");
        RetrofitFactory.clearAllRequest();
        Message message = new Message();
        message.what = 0;
        EventBus.getDefault().post(new HandleJsEvent(4002, ""));
        mHandler.sendMessageDelayed(message, jiangeXintiaoTime());
    }


    /**
     * 图片上传到服务器
     */
    private void uploadFilesService(final Record record, String id, final int type, boolean isLast) {
        ApiFactory.getFactory().create(UserApi.class).uploadImgForServer(defaultSharedPreferences.getString(Constant.BASE_ADDRESS_URL, "") + Url.UPLOAD_FILES_SERVICE, Url.XMLHTTPREQUEST, UserHelper.getToken(), UserHelper.getToken(), record.getString("ownerId"), record.getString("ownerObject"), record.getString("ownerType"), id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseData>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                      //  mHandler.sendEmptyMessage(101); //上传失败
                        XLog.i("uploadFilesService result：" + "onError:" + e, mContext);
                        EventBus.getDefault().post(new HandleJsEvent(3002, record.getString(IMAGE.OWNERID)));
                    }

                    @Override
                    public void onNext(BaseData result) {
                        Bundle bundle = new Bundle();
                        bundle.putString("OWNERID", record.getString(IMAGE.OWNERID));
                        EventBus.getDefault().post(new HandleJsEvent(3001, record.getString(IMAGE.OWNERID), record.getString(IMAGE.IMAGEURL)));
                        KLog.e("222222", record.getString(IMAGE.OWNERID) + "---" + type);
                        if (type == 0) {
                            //成功后删除图片,缩略图和数据库保存的数据  //上传image成功
                            Utility.deleteH5Image(mContext, record);
                            Message msg = mHandler.obtainMessage(100);
                            msg.obj = bundle;
                            msg.sendToTarget();

                        } else {
                            //成功后删除视频数据库保存的数据 上传vidoe成功
                            DBManager.getDBManager(mContext).delete(record);
                            Message msg = mHandler.obtainMessage(200);
                            msg.obj = bundle;
                            msg.sendToTarget();
                            // EventBus.getDefault().post(new HandleJsEvent(3001, record.getString(IMAGE.OWNERID), record.getString(IMAGE.IMAGEURL)));
                        }
                        if (isLast) {
                            KLog.e("2223005", "3005");
                            EventBus.getDefault().post(new HandleJsEvent(3005, ""));
                        }
                    }
                });
    }


    /**
     * 上传视频到客户端
     */
    private void uploadVideo(final Record record) {
        File files = new File(record.getString(VIDEO.VIDEOLOCALURL));
        if (!files.exists()) {
            return;
        }
        RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), files);
        RequestBody timeBody = RequestBody.create(MediaType.parse("multipart/form-data"), System.currentTimeMillis()+"");//,+""

        RequestBody typeBody = RequestBody.create(MediaType.parse("multipart/form-data"), record.getString(VIDEO.TIMESTAMP));
        ApiFactory.getFactory().create(UserApi.class).uploadVideo(defaultSharedPreferences.getString(Constant.BASE_ADDRESS_URL, "") + Url.UPLOAD_VIDEO, Url.XMLHTTPREQUEST, UserHelper.getToken(), requestBody, typeBody,timeBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<UploadVideoBean>() {
                    @Override
                    public void onCompleted() {
                        XLog.i("uploadImage result：" + "onCompleted", mContext);
                    }

                    @Override
                    public void onError(Throwable e) {
                        XLog.i("uploadImage result：" + "onError:" + e, mContext);
                       // mHandler.sendEmptyMessage(101); //上传失败
                        EventBus.getDefault().post(new HandleJsEvent(3002, record.getString(IMAGE.OWNERID)));
                    }

                    @Override
                    public void onNext(UploadVideoBean result) {
                        uploadFilesService(record, result.getRecord().getId() + "", 1, false);
                    }
                });
    }


    /**
     * 心跳
     */
    private void serverNginxStatus() {

        EventBus.getDefault().post(new HandleJsEvent(3003, ""));
        re_UploadImage();
//        ApiFactory.getFactory().create(UserApi.class).serverNginxStatus(defaultSharedPreferences.getString(Constant.BASE_ADDRESS_URL, "") + Url.SERVER_NGINXSTATUS)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Subscriber<BaseData>() {
//                    @Override
//                    public void onCompleted() {
//
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//                        KLog.e("HttpLogging", "onError-------------->");
//                        XLog.i("baseData:" + e);
//                        xintiaoHand();
//                    }
//
//                    @Override
//                    public void onNext(BaseData result) {
//                        if (!"200".equals(result.getStatus())) {
//                            xintiaoHand();
//                        } else {
//                            //开始上传
//                            EventBus.getDefault().post(new HandleJsEvent(3003, ""));
//                            re_UploadImage();
//                        }
//                    }
//                });
    }


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = (Bundle) msg.obj;
            KLog.e("mHandler111", msg.what + "");
            switch (msg.what) {
                case 0:
                    EventBus.getDefault().post(new HandleJsEvent(4001, ""));//开始
                    serverNginxStatus();
                    break;

                case 200: //上传video成功

                    if (mUploadSuccessBack != null) {
                        mUploadSuccessBack.uploadFileSuccess(bundle.getString("OWNERID"));
                    }

                    //开启下一组的上传
                    mCurUploadImageSuccessIndex++;
                    mHasSunccessNum++;
                    meiyizudeNum(); //重新计算每一组的数量
                    if (mCurUploadImageSuccessIndex < vedioList.size() - 1) {  //开始下一组的文件上传
                        if (mCurUploadImageSuccessIndex == nextTimeImageIndex) {
                            startUploadVideo();
                        }
                    }
                    break;
                case 100: //上传image成功

                    if (mUploadSuccessBack != null) {
                        mUploadSuccessBack.uploadFileSuccess(bundle.getString("OWNERID"));
                    }
//
//                    //开启下一组的上传
//                    mCurUploadImageSuccessIndex++;
//                    mHasSunccessNum++;
//                    meiyizudeNum(); //重新计算每一组的数量
//                    KLog.e("shuchang", "上传成功，当前文件的下标--->" + mCurUploadImageSuccessIndex);
//                    if (mCurUploadImageSuccessIndex < imagList.size() - 1) {
//                        if (mCurUploadImageSuccessIndex == nextTimeImageIndex) {
//                            startUploadImage();
//                        }
//                    } else {
//                        re_UploadVideo();
//                    }
                    break;
                case 101: //上传失败
                    //重新心跳
                    cishu = 1;
                    mHasSunccessNum = 0;
                    meiyizudeNum(); //重新计算每一组的数量
                    serverNginxStatus();
                    break;
            }
        }
    };


    /**
     * 心跳的间隔时间
     * 第一次2s，第二次4s，第三次8s，第n次 2的n次方（间隔最大值由后端字典提供，默认3600s）
     * 上传成功以后，重置间隔时间，再回到第一次2s
     *
     * @return
     */
    private long jiangeTime = 2;
    private int cishu = 1; //执行心跳的次数

    private long jiangeXintiaoTime() {
        jiangeTime = 1 << cishu;
        cishu++;
        int maxJiange = Integer.parseInt(UserHelper.getSysKeyDescValueBycode("OFFLINE_HEART_INTERVAL"));
        if (jiangeTime >= maxJiange) {
            KLog.e("shuchang1", "心跳的间隔时间---->" + maxJiange);
            return maxJiange * 1000;
        } else {
            KLog.e("shuchang1", "心跳的间隔时间---->" + jiangeTime);
            return jiangeTime * 1000;
        }

    }

    /**
     * 重新上传离线的剩余的图片
     * 先上传图片
     * 如果全部上传成功，就继续上传，如果没有全部成功那么重新发起心跳 并且每一组的数量 优化
     */
    private int lastTimeImageIndex = -1;//记录上一次上传成功之后最后一次的下标
    private int nextTimeImageIndex = -1;//记录这一组上传成功之后最后一次的下标
    private int mCurUploadImageSuccessIndex = -1; //记录当前上传成功的下标,当数值为lastTimeImageIndex时，开始下一组的上传

    private void startUploadImage() {

//        lastTimeImageIndex = nextTimeImageIndex;
//
//        if (lastTimeImageIndex < imagList.size() - 1) {
//            nextTimeImageIndex = lastTimeImageIndex + mCurMeiZuNum;
//            if (nextTimeImageIndex >= imagList.size() - 1) {
//                nextTimeImageIndex = imagList.size() - 1;
//            }
//            for (int i = 0; i < imagList.size(); i++) {
//                if (i == imagList.size()-1) {
//                    isLast=true;
//                }
//                if (i > lastTimeImageIndex && i < nextTimeImageIndex + 1) {
//                    uploadImageNew(imagList.get(i));
//                }
//            }
//        }
        imagList = DBManager.getDBManager(mContext).getRecords(IMAGE.TABLE_NAME, null);
        KLog.e("2222222222", "startUploadImage-----" + imagList.size());
        for (int i = 0; i < imagList.size(); i++) {
            uploadImageNew(imagList.get(i), i == imagList.size() - 1);
        }

    }


    private void re_UploadImage() {

        lastTimeImageIndex = -1;
        mCurUploadImageSuccessIndex = -1;

        imagList = DBManager.getDBManager(mContext).getRecords(IMAGE.TABLE_NAME, null);

        if (imagList.size() > 0) {
            startUploadImage();
        } else {
            re_UploadVideo();
        }
    }

    /**
     * 重新上传离线的剩余的视频
     * 当所有的离线图片全部上传完成之后 再上传视频
     */
    private void startUploadVideo() {
        lastTimeImageIndex = nextTimeImageIndex;

        if (lastTimeImageIndex < vedioList.size() - 1) {
            nextTimeImageIndex = lastTimeImageIndex + mCurMeiZuNum;

            if (nextTimeImageIndex >= vedioList.size() - 1) {
                nextTimeImageIndex = vedioList.size() - 1;
            }
            for (int i = 0; i < vedioList.size(); i++) {
                if (i > lastTimeImageIndex && i < nextTimeImageIndex + 1) {
                    uploadVideo(vedioList.get(i));
                }
            }
        }

    }


    private void re_UploadVideo() {

        lastTimeImageIndex = -1;
        mCurUploadImageSuccessIndex = -1;

        vedioList = DBManager.getDBManager(mContext).getRecords(VIDEO.TABLE_NAME, null);

        if (vedioList.size() > 0) {
            if (iswifiupVideo) {
                startUploadVideo();
            }
        }
    }

    /**
     * 上传的每一组的数目（即每一次上传的数目）
     * <p>
     * 第一次的每一组的数目计算方式：总数除以A，如果结果大于B，按B算
     * A，B 由 后端字典提供，默认是A默认为10，B默认为10
     * 寻找最佳的文件数量 ：第一次的每一组的数量/2，直到1
     * <p>
     * type='OFFLINE_FILE_DIVIDE'
     * 文件除数：code='FILE_DIVIDE_NUM'  每组的文件数目
     * 总组数：code='FILE_DIVIDE_GROUPS'
     * 离线心跳间隔上限：code='OFFLINE_HEART_INTERVAL'
     */
    private int allFileSize = 0; //要上传的文件的整的数目
    private int mCurMeiZuNum = 10; //当前每一组文件的数目
    private int mStartMeizuNum = 10;//记录最开始的时候的每组的文件数目（也即是第一次的）
    private int mHasSunccessNum = 0;// 记录根据当前的组上传，成功的数目,连续10次传1个没问题，每次的文件数量传2个，连续10次2个没问题，文件数量传4个，直到最初的每组文件数

    private void meiyizudeNum() {
        if (mHasSunccessNum == 10) {
            if (mCurMeiZuNum < mStartMeizuNum) {
                mCurMeiZuNum = mCurMeiZuNum * 2;
                if (mCurMeiZuNum > mStartMeizuNum) {
                    mCurMeiZuNum = mStartMeizuNum;
                }
            }
        } else if (mCurMeiZuNum > 1) {
            mCurMeiZuNum = (int) Math.ceil((double) mCurMeiZuNum / 2);
        }

        KLog.e("shuchang", "每一组的数量---->" + mCurMeiZuNum);
    }

    /**
     * 初始化每一组文件的数量
     *
     * @return
     */
    private void initMeiyizudeNum() {
        cishu = 1;
        lastTimeImageIndex = -1;//记录上一次上传成功之后最后一次的下标
        nextTimeImageIndex = -1;//记录这一组上传成功之后最后一次的下标
        mCurUploadImageSuccessIndex = -1; //记录当前上传成功的下标,当数值为lastTimeImageIndex时，开始下一组的上传

        String file_divide_num = UserHelper.getSysKeyDescValueBycode("FILE_DIVIDE_NUM");
        String file_divide_groups = UserHelper.getSysKeyDescValueBycode("FILE_DIVIDE_GROUPS");
        allFileSize = imagList.size() + vedioList.size(); //要上传的文件的整的数目
        KLog.e("shuchang11", "文件的总的数量---->" + allFileSize);

        int zushu = (int) Math.ceil(allFileSize / Double.valueOf(file_divide_num));
        if (zushu > Integer.parseInt(file_divide_groups)) {
            zushu = Integer.parseInt(file_divide_groups);
        }
        mCurMeiZuNum = (int) Math.ceil((double) allFileSize / zushu);

        mStartMeizuNum = mCurMeiZuNum;
    }

    public interface UploadSuccessBack {
        void uploadFileSuccess(String gongdanId);  //上传成功了就把工单的id传给h5
    }


    /**
     * 获取每一个工单的未上传的文件数
     *
     * @return
     */
    public List<WorkOrderNumBean> getWorkOrderList() {
        List<Record> fileList = new ArrayList<>();
        imagList = DBManager.getDBManager(mContext).getRecords(IMAGE.TABLE_NAME, null);
        vedioList = DBManager.getDBManager(mContext).getRecords(VIDEO.TABLE_NAME, null);
        fileList.addAll(imagList);
        fileList.addAll(vedioList);
        if (fileList.size() > 0) {
            List<WorkOrderNumBean> workOrderNumBeans = new ArrayList<>();
            Map<String, Integer> woMap = new ArrayMap<>();
            for (Record record : fileList) {
                String woId = record.getString("ownerId");
                if (woMap.containsKey(woId)) {
                    woMap.put(woId, woMap.get(woId) + 1);
                } else {
                    woMap.put(woId, 1);
                }
            }

            for (String key : woMap.keySet()) {
                WorkOrderNumBean workOrderNumBean = new WorkOrderNumBean();
                workOrderNumBean.setWorkOrderId(key);
                workOrderNumBean.setNumber(woMap.get(key));
                workOrderNumBeans.add(workOrderNumBean);
//                System.out.println("Key = " + key + ", Value = " + woMap.get(key));
            }

            return workOrderNumBeans;
        } else {
            return null;
        }
    }
}
